/*
 * AppProperties.java
 */

package org.mif.mobp.gui;
import java.util.Properties;
import java.io.*;
import javax.swing.JComponent;
import java.awt.Component;

/** Klase saugoja visus programos nustatymus. 
 * Nustatymus ji saugoja xml faile {@code java.io.tmpdir } kataloge. Failo pavadinimas {@code worksSettings.xml}.
 * Kiekvienas freimas ar dialogus saugoja shitame faile savo didi ir pozicija, bet to gali saugoti ir bet 
 * kokia kita informacija.
 *
 * 
 */
public class AppProperties extends Properties {
    public static final boolean DEBUG = true;
    /** Kintamasis, kuris laiko kelia iki failo */
    public static final String propertiesFile = System.getProperty("java.io.tmpdir")+"worksSettings.xml";
    /** <code>AppProperties</code> egzempliorius */
    protected static AppProperties instance;
    
    /** Creates a new instance of AppProperties 
     * Kai pasileidziamas, pakrauna properties ish failo.
     * Jei failas tushcias, nieko nedaro.
     */
    protected AppProperties() {
        super ();
        checkFileForExistance();
        File file = new File (propertiesFile);
        try {
            if (file.length() != 0) { //tikrinam jeigu faile kas nors irashyta, ir tik tada uzkraunam properties
                FileInputStream stream = new FileInputStream (file);
                loadFromXML(stream);
                stream.close();
            }
        }
        catch (FileNotFoundException ex) {
            System.out.println ("1" + ex.getMessage());
        }
        catch (IOException ex) {
            System.out.println ("2" + ex.getMessage());
        }
    }
    /** Grazina <code>AppProperties</code> egzemplioriu.
     *  Visoje programoje gali buti tik vienas shios klases egzempliorius */
    public static AppProperties getInstance () {
        if (instance == null) { //Tikrinam ar jau nebuvo inicializuotas anksciau
            instance = new AppProperties (); //Jei ne kuriam nauja objekta
        }
        return instance;
    }
    /** Tikrina ar jau yra sukurtas failas su nustatymais.
     * Jei nera, sukuria ji */
    private void checkFileForExistance () {
        File file = new File (propertiesFile);
        if (!file.exists()) {  //failas dar neegzistuoja, sukuriam ji
            try {
                file.createNewFile();
            }
            catch (IOException ex) {
                if (DEBUG) {
                    System.out.println ("3 Can not create file " + propertiesFile);
                    System.out.println (ex.getMessage());
                }
            }
        }
    }
    /** Ishsaugoja visus nustatymus i faila XML formate*/
    public void saveSettings () {
        checkFileForExistance();
        File file = new File (propertiesFile);
        
        try {
            FileOutputStream stream = new FileOutputStream (file);
            storeToXML(stream, "Works app settings");
        }
        catch (FileNotFoundException ex) {
            if (DEBUG)
                System.out.println ("4" + ex.getMessage());
        }
        catch (IOException ex) {
            if (DEBUG)
                System.out.println ("5" + ex.getMessage());
        }
        
        
    }
    /** Kiekvienas freimas, arba dialogas saugos nustatymus apie lango dydi ir jo pozicija.
     * Sekancios dvi funkcijos sukurtos del patogumo, kad galima butu paprasciau ishsaugoti shiuos parametrus.
     * @param frameName identifikuoja freima, pvz: MainFrame. Parametrai faile bus saugomi kaip FrameName.sizeX , sizeY
     * width ir height.
     * @param component komponentas kurio parametrus saugosim
     */
    public void putSizePos (String frameName, Component component) {
        double width = component.getSize().getWidth(),
               height = component.getSize ().getHeight(),
               posX = component.getLocation().getX(),
               posY = component.getLocation().getY();
        setProperty(frameName + ".width", ""+width);
        setProperty(frameName + ".height", ""+height);
        setProperty(frameName + ".posX", ""+posX);
        setProperty(frameName + ".posY", ""+posY);
        saveSettings();
    }
    /** Grazina freimo nustatymus
     * @param frameName freimo vardas
     * @param component komponentas kuriam reikia pakeist parametrus
     */
    public void getSizePos (String frameName, Component component) {
        double width, height, posX, posY;
        width = Double.parseDouble(getProperty(frameName + ".width", "300"));
        height = Double.parseDouble(getProperty(frameName + ".height", "300"));
        posX = Double.parseDouble(getProperty(frameName + ".posX", "100"));
        posY = Double.parseDouble(getProperty(frameName + ".posY", "100"));
        component.setLocation(new java.awt.Point((int)posX, (int)posY));
        component.setSize((int)width, (int)height);
    }
    
}
